package com.study.controller;

//import com.study.domain.User2;
//import com.study.mapper.User2Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

@Controller
public class LoginController {

    @GetMapping("/403Page")
    public String access403Page(Model model){

        String msg = "您的权限不足以访问，请联系管理员";
        model.addAttribute("msg", msg);
        return "unauth";
    }
    @GetMapping("/unauth")
    public String accessDenyPage(Model model){

        String msg = "用户名或者密码错误，请重新输入";
        model.addAttribute("msg", msg);
        return "login";

    }

    @GetMapping("/toLoginPage")
    public String hello(Model model) {
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }


    /*
     下面代码是演示没有Security的情况下，我们如何校验登录的。
     当我们有Security之后，这个方法就作废了
     */

    /*
    @Autowired
    User2Mapper user2Mapper;
    //@GetMapping("/login")

    public String login(Model model, HttpServletRequest request) {

        System.out.println("login进入");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username=>"+username);
        //Todo 从这里开始去连接数据库校验
        User2 user2 = user2Mapper.findByNameAndPassword(username, password);
        System.out.println("user2=>"+user2);
        if (user2 == null) {
            //    登录失败，给用户提示
            String msg = "用户名或者密码错误，请重新输入";
            model.addAttribute("msg", msg);
            return "login";
        } else {
            //   成功就跳转
            model.addAttribute("username", username);
            return "list";
        }
    }
    */
}
